package pl.taskman.view.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JTextField;

import pl.taskman.helper.ColorHelper;

public class CustomTextField extends JTextField
{
	/**
	 *	Generated serial ID 
	 */
	private static final long	serialVersionUID	= 5058280377476888725L;
	private Color				color				= null;
	
	public CustomTextField(Color color)
	{
		super();
		this.color = color;
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		setCaretColor(Color.WHITE);
		setSelectedTextColor(new Color(20, 20, 205, 205));
	}
	
	@Override
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gradient = null;
		
		gradient = new GradientPaint((getWidth() / 2), 0, color, (getWidth() / 2), (int)(getHeight() / 0.8), ColorHelper.darken(color, 50), false);
		
		// FIXME Add custom carret and selection
		g2.setPaint(gradient);
		g2.fillRoundRect(1, 1, getWidth(), getHeight(), 10, 10);
		
		int fontSize = (int) Math.floor((getHeight() / 3)) + 2;
		
		g2.setFont(new Font("Dialog", Font.BOLD, fontSize));
		FontMetrics metrics = g2.getFontMetrics();
		Rectangle2D rect = metrics.getStringBounds(getText(), g2);

		double height = rect.getHeight();
		
		int posX = 10;
		int poxY = (int)((getHeight() / 2) + (height / 2) - 5);
		
		g2.setColor(Color.WHITE);
		g2.drawString(getText(), posX, poxY);
	}
}
